﻿using System.ComponentModel;
using System.Drawing;
using Newtonsoft.Json;
using ProjectN.Core.Element.Def;
using ProjectN.Core.Instance.Data;

namespace ProjectN.Core.Element
{
    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class Text : BasicElement
    {
        [Category("基本"), DisplayName("文本"), JsonProperty]
        public required string Context { get; set; }

        [Category("基本"), DisplayName("位置"), JsonProperty]
        public required PointF Position { get; set; }

        [Category("基本"), DisplayName("字体"), JsonProperty]
        public required FontInfo Font { get; set; }

        [Category("基本"), DisplayName("填充"), JsonProperty]
        public required BrushInfo Brush { get; set; }

        public Text()
        {
            this.Load += Text_Load;
            this.Draw += Text_Draw;
        }

        private void Text_Draw(object sender, DrawEventArgs e)
        {
            e.Graphics.DrawString(Context, Font.Create(), Utilities.ColoredBrush(Brush.Create(), e.Color), (System.Drawing.PointF)Position);
        }

        private void Text_Load(object sender, LoadEventArgs e)
        {
            Data = e.InstanceData;
        }

        protected override void UpdateData(InstanceData data)
        {
            Brush.Data = data;
        }
        public override string ToString() => "文本";
    }

    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class RectedText : BasicElement
    {
        private static readonly Brush BRUSH = new SolidBrush(Color.FromArgb(0));
        private static readonly Pen PEN = new(BRUSH);

        [Category("基本"), DisplayName("文本"), JsonProperty]
        public required string Context { get; set; }

        [Category("基本"), DisplayName("位置"), JsonProperty]
        public required Point Position { get; set; }

        [Category("基本"), DisplayName("填充"), JsonProperty]
        public required BrushInfo Brush { get; set; }

        [Category("基本"), DisplayName("字体"), JsonProperty]
        public required FontInfo Font { get; set; }

        [Browsable(false), JsonProperty]
        public required int BorderWidth { get; set; }
        public RectedText()
        {
            this.Load += RectedText_Load;
            this.Draw += RectedText_Draw;
        }

        private void RectedText_Draw(object sender, DrawEventArgs e)
        {
            e.Graphics.FillRectangle(Utilities.ColoredBrush(BRUSH, e.Color),
                new System.Drawing.Rectangle(System.Drawing.Point.Subtract((System.Drawing.Point)Position, new System.Drawing.Size(BorderWidth, BorderWidth)), System.Drawing.Size.Add(System.Drawing.Size.Ceiling(e.Graphics.MeasureString(Context, Font.Create())), new System.Drawing.Size(2 * BorderWidth, 2 * BorderWidth)))
            );
            e.Graphics.DrawString(Context, Font.Create(), Utilities.ColoredBrush(Brush.Create(), e.Color), (System.Drawing.Point)Position);
        }

        private void RectedText_Load(object sender, LoadEventArgs e)
        {
            Data = e.InstanceData;
        }

        protected override void UpdateData(InstanceData data)
        {
            Brush.Data = data;
        }

        public override string ToString() => "文本";
    }
}
